<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">

    <link rel="stylesheet" href="/gupiao/css/weui.css">
    <link rel="stylesheet" href="/gupiao/css/jquery-weui.css">
    <link rel="stylesheet" href="/gupiao/css/style.css">
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_689900_tgundrn0e1a46lxr.css">

    <style>
        .recharge-box{padding:30px;margin-bottom: 30px;}
        .recharge-box input{border:0;}
        .recharge-box .f15{margin-bottom: 30px;}
        .recharge-box .borYuan{border-bottom: 1px solid #ddd;padding-bottom: 10px;}
        .release-bottom{margin: 5px;}
    </style>
</head>
<body class="back1">


<div class="index-top-fix">
    <a href="{:url('index/gupiao.mycenter/mycenter')}" class="iconfont icon-left- left"></a>
    <p class="title">充值</p>
</div>

<div class="mt50"></div>
<form action="" method="post" id="money" >

<div class="index-box recharge-box">
    <p class="f15">充值金额</p>
    <div class="borYuan">
        <span class="f20">￥</span>
        <input type="text" name="money" id="re_money" placeholder="最低充值 {$web_config.min_recharge} 元" value="{$web_config.min_recharge}" >
    </div>
</div>
</form>

<div class="release-bottom">
    <a href="javascript:;" onclick="recharge()" class="weui-btn qrfb-btn">确认充值</a>
</div>

</body>
</html>
<script src="/gupiao/js/jquery.min.js"></script>
<script src="/gupiao/js/jquery-weui.min.js"></script>
<script src="/res/lib/wx/jweixin-1.2.0.js"></script>
<script>

    // 充值金额不能小于最低充值金额
    $("#re_money").blur(function(){
        var re_money        = $("#re_money").val();
        var min_recharge    = Number('{$web_config.min_recharge}');
        if(re_money < min_recharge){
            alert("充值金额不得小于"+min_recharge+'元');
            $("#re_money").val(min_recharge);
            return;
        }
    });

    // 新建点评
    function recharge() {

        $.ajax({
            type: "POST",                       //方法类型
            dataType: "json",                   //预期服务器返回的数据类型
            url: "{:url('gupiao.users/recharge')}" ,        //url
            data: $('#money').serialize(),
            success: function (res) {
                if (res.code == 200) {
                    var order_id = res.order_id;


                    if(typeof res.data === "string"){
                        var ret = JSON.parse(res.data);
                    }else{
                        var ret = res.data;
                    }

                    // 在这里要写一个轮询 是用来查询是否支付成功
                    setInterval(function () {
                        checkIsExist(order_id)
                    },1000);

                    callpay(ret);
                };
            },
            error : function() {
                alert("异常！");
            }
        });
    }

    function jsApiCall(jsApiParameters)
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            jsApiParameters,
            function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    // 使用以上方式判断前端返回,微信团队郑重提示：
                    // res.err_msg将在用户支付成功后返回
                    // ok，但并不保证它绝对可靠。
                    //跳转页面window.localtion.href='';
                    window.localtion.href="{:url('index/index/joinlive')}";
                }else{
                    // alert('支付出现错误！'+res.err_code+res.err_desc+res.err_msg);
                    // alert(res.err_code+res.err_desc+res.err_msg); // 显示错误信息
                }
            }
        );
    }

    function callpay(jsApiParameters)
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall(jsApiParameters);
        }
    }

    function checkIsExist(order_id){
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "{:url('gupiao.users/checkRecharge')}" ,
            data: {order_id:order_id},
            success: function(res){
                var data = res.data;
                if(res.code == 200){
                    if(data.status == 1){
                        alert('充值成功');
                        location.href ="{:url('gupiao.index/index')}";
                    }
                }
            }
        });
    }
</script>

<script>



</script>
